package com.cms.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.cms.database.SQLString;

public class SequenceHelper {
	
	public static final String RSF_SEQ = "rsf";
	public static final String RSF_ATTACHMENT_SEQ = "rsf_attachment";
	public static final String PRODUCTS_SEQ = "products";
	public static final String PRODUCTS_PICTURES_SEQ = "product_pictures";
	
	
	@SuppressWarnings("finally")
	public static int getSeqNextVal(Connection conn,String seq_name){
		PreparedStatement pst = null;
		ResultSet rs = null;
		int seq_id = 0;
		try {
			pst = conn.prepareStatement(SQLString.GET_NEXT_SEQ_ID);
			pst.setString(1, seq_name);
			rs = pst.executeQuery();
			while (rs.next()) {
				seq_id = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (final SQLException e) {
				}
			}
			if (pst != null) {
				try {
					pst.close();
					pst = null;
				} catch (final SQLException e) {
				}
			}
			return seq_id;
		}
	}

	@SuppressWarnings("finally")
	public static int getSeqCurrVal(Connection conn,String seq_name){
		PreparedStatement pst = null;
		ResultSet rs = null;
		int seq_id = 0;
		try {
			pst = conn.prepareStatement(SQLString.GET_CURR_SEQ_ID);
			pst.setString(1, seq_name);
			rs = pst.executeQuery();
			while (rs.next()) {
				seq_id = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (final SQLException e) {
				}
			}
			if (pst != null) {
				try {
					pst.close();
					pst = null;
				} catch (final SQLException e) {
				}
			}
			return seq_id;
		}
	}
}
